package com.sunny.ynhz.ui.patrol.problems;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.sunny.ynhz.R;
import com.sunny.ynhz.bean.ProblemBean;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by luao on 2017/7/23.
 */

public class PositionMapSelectActivity extends AppBaseTranslationTitleBarActivity implements GeocodeSearch.OnGeocodeSearchListener {
    @BindView(R.id.select_map)
    MapView selectMap;
    @BindView(R.id.addr_str)
    TextView addrStr;
    @BindView(R.id.btns)
    LinearLayout btns;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.addvd)
    TextView addvd;
    private AMap aMap;
    private Marker locationMarker;
    private GeocodeSearch geocoderSearch;
    private ProblemBean.DataBean dataBean;
    private double lttd;
    private double lgtd;
    private String position;
    private String los2;
    private String addvcd;

    private void doAct(Context context) {
        Intent intent = new Intent(context, PositionMapSelectActivity.class);
        intent.putExtra("position", "");
        ((Activity) context).startActivityForResult(intent, 200);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_map_select);
        ButterKnife.bind(this);
//        selectMap.onSaveInstanceState(savedInstanceState);
        selectMap.onCreate(savedInstanceState);
        aMap = selectMap.getMap();
        aMap.setTrafficEnabled(false);// 显示实时交通状况
        aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 标准地图模式

        Intent intent = getIntent();
        position = intent.getStringExtra("position");
        lttd = intent.getDoubleExtra("lttd", 0.0);
        lgtd = intent.getDoubleExtra("lgtd", 0.0);
        String text = "";


        changeCamera(
                CameraUpdateFactory.newCameraPosition(
                        new CameraPosition(
                                new LatLng(lttd, lgtd), 18, 0, 0)));
        locationMarker = aMap.addMarker(new MarkerOptions().position(new LatLng(lttd, lgtd)).title("北京").snippet("DefaultMarker"));

        aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                locationMarker.setPosition(cameraPosition.target);

            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
                //cameraPosition是屏幕中心的位置信息
                locationMarker.setPosition(cameraPosition.target);
                getAddress(new LatLonPoint(cameraPosition.target.latitude, cameraPosition.target.longitude));
                lttd = locationMarker.getPosition().latitude;
                lgtd = locationMarker.getPosition().longitude;

            }
        });
        geocoderSearch = new GeocodeSearch(this);
        geocoderSearch.setOnGeocodeSearchListener(this);

    }

    @OnClick({R.id.cancel, R.id.confirm})
    public void onClick(View view) {
        if (view.getId() == R.id.cancel) {
            finish();
        } else if (view.getId() == R.id.confirm) {
            Intent intent = new Intent();
            intent.putExtra("position", los2);
            intent.putExtra("lttd", lttd);
            intent.putExtra("lgtd", lgtd);
            intent.putExtra("addvcd", addvcd);
            setResult(200, intent);
            finish();
        }
    }

    /**
     * 根据动画按钮状态，调用函数animateCamera或moveCamera来改变可视区域
     */
    private void changeCamera(CameraUpdate update) {
        aMap.moveCamera(update);
    }


    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        selectMap.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        selectMap.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        selectMap.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        selectMap.onDestroy();
    }

    /**
     * 响应逆地理编码
     */
    public void getAddress(final LatLonPoint latLonPoint) {

        RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200,
                GeocodeSearch.AMAP);// 第一个参数表示一个Latlng，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
        geocoderSearch.getFromLocationAsyn(query);// 设置异步逆地理编码请求
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        if (i == AMapException.CODE_AMAP_SUCCESS) {
            if (regeocodeResult != null && regeocodeResult.getRegeocodeAddress() != null
                    && regeocodeResult.getRegeocodeAddress().getFormatAddress() != null) {

                addvcd = regeocodeResult.getRegeocodeAddress().getAdCode();
                addvd.setText("行政区划编码" + addvcd);

                los2 = regeocodeResult.getRegeocodeAddress().getFormatAddress()
                        + "(" + regeocodeResult.getRegeocodeAddress().getDistrict() + "" + regeocodeResult.getRegeocodeAddress().getTownship()
                        + "" + regeocodeResult.getRegeocodeAddress().getStreetNumber().getStreet()
                        + "" + regeocodeResult.getRegeocodeAddress().getStreetNumber().getNumber() + "" + regeocodeResult.getRegeocodeAddress().getBuilding() + ")附近";
                addrStr.setText("选择位置：" + los2);
                dataBean.setPosition(los2);
                //// TODO: 2017/8/11 下面这两句话会导致函数不继续执行？regeocodeResult.getRegeocodeQuery().getPoint().getLatitude());不能调用，调了就会导致函数不继续执行，原因待定
//                dataBean.setLttd(regeocodeResult.getRegeocodeQuery().getPoint().getLatitude());
//                dataBean.setLgtd(regeocodeResult.getRegeocodeQuery().getPoint().getLongitude());
            } else {
//                ToastUtil.show(ReGeocoderActivity.this, R.string.no_result);
            }
        } else {
//            ToastUtil.showerror(this, i);
        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }
}
